<template>
  <j-modal
    title="产品入库快捷录入"
    :width="width"
    :visible="visible"
    :confirmLoading="confirmLoading"
    okText="确定录入"
    switchFullscreen
    @cancel="handleCancel"
    @ok="handleOk"
    :body-style="{
      padding: '10px'
    }"
    cancelText="关闭"
  >
    <div style="padding-bottom:10px; display: grid; justify-items: end; grid-template-columns: auto 1fr;">
      <div>
        <a-dropdown v-if="selectedRowKeys.length > 0">
          <a-button style="background: #ff4757;color: white">
            批量删除
            <a-icon type="delete" />
          </a-button>
        </a-dropdown>
      </div>
      <div>
        <a-button v-if="!editable" type="primary" icon="edit" @click="handleEditBatch">编辑全部数量</a-button>
        <a-button
          v-else
          type="primary"
          icon="save"
          style="background-color: #2ed573;border-color: #67c23a;"
          @click="handleSaveBatch"
          >保存全部
        </a-button>
      </div>
    </div>

    <a-table
      ref="table"
      size="middle"
      bordered
      rowKey="id"
      :scroll="{ x: true }"
      :columns="columns"
      :dataSource="dataSource"
      :pagination="false"
      :rowSelection="{ selectedRowKeys: selectedRowKeys, onChange: onSelectChange }"
    >
      <template v-for="slot in slots" :slot="slot" slot-scope="text, record">
        <a-input-number v-if="editable" v-model:value="record[slot]" :min="0" />
        <span v-else>{{ record[slot] }}</span>
      </template>

      <!--      <template slot='goodCount' slot-scope='text, record'>-->
      <!--        {{ record.goodCount }}-->
      <!--      </template>-->
    </a-table>
  </j-modal>
</template>

<script>
import { getAction, postAction } from '@api/manage'

export default {
  name: 'ProductInQuickInput',
  components: {},
  props: {
    sourceId: {
      type: String,
      default: ''
    },
    sourceSerial: {
      type: String,
      default: ''
    }
  },
  watch: {},
  data() {
    return {
      width: 1400,
      visible: false,
      confirmLoading: false,
      editable: true,
      slots: ['goodCount', 'defectiveCount', 'scrapCount'],
      columns: [
        {
          title: '#',
          dataIndex: '',
          key: 'rowIndex',
          width: 60,
          align: 'center',
          customRender: function(t, r, index) {
            return parseInt(index) + 1
          }
        },
        {
          title: '物料代码',
          align: 'center',
          dataIndex: 'materialCode',
          scopedSlots: { customRender: 'materialCode' }
        },
        {
          title: '物料名称',
          align: 'center',
          dataIndex: 'materialName'
        },
        {
          title: '规格型号',
          align: 'center',
          dataIndex: 'sap'
        },
        {
          title: '单位',
          align: 'center',
          dataIndex: 'unit_dictText'
        },
        {
          title: '物料类型',
          align: 'center',
          dataIndex: 'materialType_dictText'
        },
        {
          title: '发料仓库',
          align: 'center',
          dataIndex: 'warehouse_dictText',
          scopedSlots: { customRender: 'warehouse' }
        },
        {
          title: '良品',
          align: 'center',
          dataIndex: 'goodCount',
          scopedSlots: { customRender: 'goodCount' }
        },
        {
          title: '不良品',
          align: 'center',
          dataIndex: 'defectiveCount',
          scopedSlots: { customRender: 'defectiveCount' }
        },
        {
          title: '报废品',
          align: 'center',
          dataIndex: 'scrapCount',
          scopedSlots: { customRender: 'scrapCount' }
        },
        {
          title: '操作',
          dataIndex: 'action',
          align: 'center',
          fixed: 'right',
          width: 147,
          scopedSlots: { customRender: 'action' }
        }
      ],
      url: {
        quickInputDetail: '/production/hfProductionProductInput/quickInputDetail'
      },
      selectedRowKeys: [],
      dataSource: []
    }
  },
  methods: {
    loadData(missionDetailList) {},
    onSelectChange(selectedRowKeys, selectionRows) {
      this.selectedRowKeys = selectedRowKeys
      this.selectionRows = selectionRows
    },
    close() {
      this.$emit('close')
      this.visible = false
      this.currentStep = 1
    },
    handleCancel() {
      this.close()
    },
    handleOk() {
      let data = []
      for (const item of this.dataSource) {
        if (item['goodCount'] !== 0) {
          let temp = Object.assign({}, item)
          temp['actualInputNumber'] = item['goodCount']
          temp.id = item['goodId']
          data.push(temp)
        }
        if (item['defectiveCount'] !== 0) {
          let temp = Object.assign({}, item)
          temp['actualInputNumber'] = item['defectiveCount']
          temp.id = item['defectiveId']
          data.push(temp)
        }
        if (item['scrapCount'] !== 0) {
          let temp = Object.assign({}, item)
          temp['actualInputNumber'] = item['scrapCount']
          temp.id = item['scrapId']
          data.push(temp)
        }
      }

      postAction(this.url.quickInputDetail, data, { params: { sourceId: this.sourceId } }).then(res => {
        if (res.success) {
          this.$message.success('提交成功')
          this.close()
          this.$emit('success')
        } else {
          this.$message.error(res.message)
        }
      })
    },
    handleEditBatch() {
      if (this.dataSource.length === 0) {
        this.$message.warning('请先添加至少一条明细')
        return
      }
      this.editable = true
    },
    handleSaveBatch() {
      this.editable = false
    },
    batchDel() {}
  }
}
</script>

<style scoped></style>
